﻿using System;
using System.Globalization;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Media.Animation;
using System.Windows.Media;
using System.Windows;
using System.Linq;

namespace BMSPlatform.Control
{
    public class Switcher : ToggleButton
    {
        private TranslateTransform ballMoveX;
        static Switcher()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(Switcher), new FrameworkPropertyMetadata(typeof(Switcher)));
        }

        #region Propdp
        public static readonly DependencyProperty CheckedTipProperty =
            DependencyProperty.Register("CheckedTip", typeof(string), typeof(Switcher), new PropertyMetadata("ON"));
        public static readonly DependencyProperty UncheckedTipProperty =
            DependencyProperty.Register("UncheckedTip", typeof(string), typeof(Switcher), new PropertyMetadata("OFF"));
        public static readonly DependencyProperty BallColorProperty =
            DependencyProperty.Register("BallColor", typeof(Brush), typeof(Switcher), new PropertyMetadata(new SolidColorBrush(Color.FromRgb(0xFF,0xFF,0xFF))));

        public string CheckedTip
        {
            get { return (string)GetValue(CheckedTipProperty); }
            set { SetValue(CheckedTipProperty, value); }
        }

        public string UncheckedTip
        {
            get { return (string)GetValue(UncheckedTipProperty); }
            set { SetValue(UncheckedTipProperty, value); }
        }
        public Brush BallColor
        {
            get { return (Brush)GetValue(BallColorProperty); }
            set { SetValue(BallColorProperty, value); }
        }
        #endregion

        public Switcher() : base()
        {
            Checked += CheckedAnimation;
            Unchecked += UncheckedAnimation;
            Loaded += (e, s) =>
            {
                ballMoveX = ((Switcher)e).GetTemplateChild("trans") as TranslateTransform;
                if (IsChecked != null && ballMoveX != null) ballMoveX.X = (IsChecked == true ? -1 : 1) * (ActualWidth - ActualHeight) / 2;
            };
        }

        #region Animations
        DoubleAnimation ballAnima = new DoubleAnimation(0, new Duration(new TimeSpan(0, 0, 0, 0, 300)))
        {
            EasingFunction = new CubicEase() { EasingMode = System.Windows.Media.Animation.EasingMode.EaseInOut }
        };

        private void CheckedAnimation(object sender, RoutedEventArgs e)
        {
            if (!IsLoaded) e.Handled = true;//未初始化成功时，不向外传递Checked事件
            if (ballMoveX == null) return;
            ballAnima.To = -(ActualWidth - ActualHeight) / 2;
            ballMoveX.BeginAnimation(System.Windows.Media.TranslateTransform.XProperty, ballAnima);
        }
        private void UncheckedAnimation(object sender, RoutedEventArgs e)
        {
            if (!IsLoaded) e.Handled = true;//未初始化成功时，不向外传递Unchecked事件
            if (ballMoveX == null) return;
            ballAnima.To = (ActualWidth - ActualHeight) / 2;
            ballMoveX.BeginAnimation(System.Windows.Media.TranslateTransform.XProperty, ballAnima);
        }
        #endregion

        // 递归查找指定类型的元素
        private DependencyObject FindElementInResources(ResourceDictionary resources, Type elementType)
        {
            // 遍历资源字典中的每个资源
            foreach (var resource in resources.Values)
            {
                if (resource is FrameworkElement frameworkElement)
                {
                    if (frameworkElement.GetType() == elementType)
                    {
                        // 找到了元素
                        return frameworkElement;
                    }

                    // 递归查找子元素
                    var element = FindElementInResources(frameworkElement.Resources, elementType) as FrameworkElement;

                    if (element != null)
                    {
                        // 找到了元素
                        return element;
                    }
                }
            }

            // 没有找到元素
            return null;
        }
    }

    #region Converters
    /// <summary>
    /// 根据控件高度设置控件圆角半径
    /// 圆角半径为高度的二分之一
    /// </summary>
    public class HeightCornerRadiusConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return 0.5 * int.Parse(value.ToString());
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// 当Ischecked=True时,
    /// 隐藏CheckedTip文本
    /// </summary>
    public class CheckedToVisbilityConvert : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null) return Visibility.Hidden;
            return (bool)value ? Visibility.Visible : Visibility.Hidden;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// 当Ischecked=False时,
    /// 隐藏UncheckedTip文本
    /// </summary>
    public class CheckedToHidenConvert : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null) return Visibility.Hidden;
            return (bool)value ? Visibility.Hidden : Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    #endregion
}
